package com.ocom.payhandle.controller;

import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.PayHandleUrl;
import com.ocom.common.entity.access.EntityDeptInfo;
import com.ocom.common.entity.card.*;
import com.ocom.common.entity.company.EntityCompanyConfig;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.entity.payconfig.EntityXfDeviceMealConfig;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payhandle.QrCardPayRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.pyhandle.AnalyzeBankUsersVo;
import com.ocom.common.vo.pyhandle.FeePlanVo;
import com.ocom.payhandle.fun.PayFunService;
import com.ocom.payhandle.fun.PayHandleFunService;
import com.ocom.payhandle.service.*;
import com.ocom.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RefreshScope
@Slf4j
@RestController
public class OnlinePayCardController {

    @Autowired
    DeviceInfoService deviceInfoService;

    @Autowired
    XfPointInfoService xfPointInfoService;

    @Autowired
    XfChannelInfoService xfChannelInfoService;

    @Resource
    private RedisUtil redisUtil;

    @Autowired
    private XfAccountTypeService xfAccountTypeService;

    @Autowired
    private XfCardInfoService xfCardInfoService;

    @Autowired
    private XfAccountInfoService xfAccountInfoService;

    @Autowired
    private PersonnelInfoService personnelInfoService;

    @Autowired
    private DeptInfoService deptInfoService;

    @Autowired
    private XfDiscountConfigServcie xfDiscountConfigServcie;

    @Autowired
    private XfUserparamConfigService xfUserparamConfigService;

    @Autowired
    private ConsTypeConfigService consTypeConfigService;

    @Autowired
    PayHandleFunService payHandleFunService;

    @Autowired
    PayFunService payFunService;
    @Autowired
    private XfSubAccountListService xfSubAccountListService;

    @Autowired
    private CompanyConfigService companyConfigService;

    @Autowired
    XfRecordProcessExxxService xfRecordProcessExxxService;

    //在线卡消费
//    @PostMapping(PayHandleUrl.getPayCardC)
    @PostMapping(PayHandleUrl.getPayCard)
    public Result onlinePayCard(@RequestBody QrCardPayRequest request) {
        log.info("【强在线】 lBWJXo9X 【{}】【{}】 【{}】",  request.getMachNo(),request.getTradeNo(),request);
        try {
            //支付宝支付权限判断
            if (request.getType() == 9 && !request.getFaceToken().startsWith("AAAA")) {
                return ResultUtil.error(ResultEnum.Err_3085);
            }

            String tradeNoRedis = "onlinePay:" + request.getCompanyId() + ":" + request.getMachNo() + ":" + request.getTradeNo();
            if (redisUtil.lock(tradeNoRedis, 60 * 60)) {
                //判断是什么支付类型
                //1-刷卡，4-刷脸，5-付款码，7-ocom离线码 8微信人脸
                EntityXfAccountInfo entityXfAccountInfo = null;
                EntityPersonnelInfo entityPersonnelInfo = null;
                EntityXfRecordProcess entityXfRecordProcess = new EntityXfRecordProcess();
                EntityCardInfo entityCardInfo = null;
                if (request.getType() == 1) {
                    //刷卡
                    entityCardInfo = xfCardInfoService.selectCard(request.getCompanyId(), request.getAuthCode());
                    if (entityCardInfo == null) {
                        return ResultUtil.error(ResultEnum.Err_305);
                    }
                    if (entityCardInfo.getCardState() != 1) {
                        return ResultUtil.error(ResultEnum.Err_4021);
                    }
                    String strNewDate = DateUtils.getStrDate(new Date(), "yyyy-MM-dd");
                    String strEndDate = DateUtils.getStrDate(entityCardInfo.getEndDate(), "yyyy-MM-dd");
                    if (!DateUtils.compare(strNewDate, strEndDate)) {
                        return ResultUtil.error(ResultEnum.Err_4025);
                    }
                    //用账号查询账号表和人员的信息
                    entityXfAccountInfo = xfAccountInfoService.selectAccount(request.getCompanyId(), entityCardInfo.getAccountNo());
                    entityPersonnelInfo = personnelInfoService.selectPersonnel(request.getCompanyId(), entityCardInfo.getPId());

                    //操作类型：
                    //1：刷卡
                    //2：付款码支付
                    //3：免密代扣支付
                    //4：主扫
                    //6：优惠卷
                    //9：订餐
                    //11：现金充值
                    //12：线上充值
                    entityXfRecordProcess.setOpType(1l);
                    entityXfRecordProcess.setAccountNo(entityCardInfo.getAccountNo());
                } else {

                    if (request.getType() == 8) {
                        EntityPersonnelInfo entityPersonnelInfo1 = personnelInfoService.getPersonByPhone(request.getCompanyId(), request.getAuthCode());
                        if (entityPersonnelInfo1 == null) {
                            return ResultUtil.error(ResultEnum.Err_4017);
                        }
                        request.setAuthCode(entityPersonnelInfo1.getPId().toString());
                        if (request.getFaceToken() == null) {
                            return ResultUtil.error(ResultEnum.Err_4027);
                        }
                    }
                    Long pId = 0l;
                    if (request.getType() == 21) {
                        if (request.getAuthCode().startsWith("CT")) {
                            AnalyzeBankUsersVo analyzeBankUsersVo = new AnalyzeBankUsersVo();
                            analyzeBankUsersVo.setCompanyId(request.getCompanyId());
                            analyzeBankUsersVo.setAuthCode(request.getAuthCode());
                            pId = payFunService.AnalyzeBankUsers(analyzeBankUsersVo);
                            if (pId <= 0l) {
                                return ResultUtil.error(ResultEnum.Err_4017);
                            }
                        } else {
                            pId = Long.valueOf(request.getAuthCode());
                        }
                    } else {
                        pId = Long.valueOf(request.getAuthCode());
                    }
                    entityPersonnelInfo = personnelInfoService.selectPersonnel(request.getCompanyId(), pId);
                    entityXfAccountInfo = xfAccountInfoService.selectAccountPid(request.getCompanyId(), pId);
                    entityXfRecordProcess.setAccountNo(entityXfAccountInfo.getAccountNo());
                    if (request.getType() == 4) {
                        entityXfRecordProcess.setOpType(22l);
                    } else if (request.getType() == 7) {
                        entityXfRecordProcess.setOpType(21l);
                    } else if (request.getType() == 8) {
                        //8 微信人脸代扣 22人脸
                        entityXfRecordProcess.setOpType(22l);
                    } else if (request.getType() == 25) {
                        //掌纹
                        entityXfRecordProcess.setOpType(25l);
                    }
                }

                if (entityXfAccountInfo == null) {
                    return ResultUtil.error(ResultEnum.Err_816);
                }
                if (entityXfAccountInfo.getAccountState() != 0) {
                    return ResultUtil.error(ResultEnum.Err_4022);
                }

                if (entityPersonnelInfo == null) {
                    return ResultUtil.error(ResultEnum.Err_4017);
                }
                if (entityPersonnelInfo.getStatus() != 0) {
                    return ResultUtil.error(ResultEnum.Err_4023);
                }

                entityXfRecordProcess.setComId(request.getCompanyId());
                entityXfRecordProcess.setDeviceNo(request.getMachNo());
                entityXfRecordProcess.setCardNo(request.getAuthCode());
                entityXfRecordProcess.setAccountNo(entityXfAccountInfo.getAccountNo());
                entityXfRecordProcess.setPId(entityXfAccountInfo.getPId());
                entityXfRecordProcess.setPCode(entityPersonnelInfo.getPCode());
                entityXfRecordProcess.setPName(entityPersonnelInfo.getPName());
                entityXfRecordProcess.setAccTypeId(entityXfAccountInfo.getAccTypeId());
                Date date = new Date();
                entityXfRecordProcess.setCreateTime(date);
                entityXfRecordProcess.setMonthValue(DateUtils.getStrDate(date, "yyyyMM"));
                //查询收费的和商家信息
                payHandleFunService.relatedDevice(entityXfRecordProcess, request.getCompanyId(), request.getMachNo(), date);
                if(request.getConsType() == 1 && entityXfRecordProcess.getMealNo().equals("0")){
                    EntityCompanyConfig entityCompanyConfig1 = companyConfigService.getTapValue(request.getCompanyId(), "outOfMealConsumption");
                    if (entityCompanyConfig1 != null && "true".equals(entityCompanyConfig1.getConfig())) {
                        return ResultUtil.error(ResultEnum.Err_3116);
                    }
                }
                if (!payHandleFunService.checkCanPay(request.getConsType(),entityXfRecordProcess, entityXfAccountInfo)) {
                    return ResultUtil.error(ResultEnum.Err_3098);
                }

                if (request.getConsState() == 3 || request.getConsState() == 5) {
                    payHandleFunService.relatedReceiptNo(entityXfRecordProcess);
                    if (request.getConsState() == 5) {
                        payHandleFunService.relatedDingCan(entityXfRecordProcess, request.getConsDate(), request.getConsMeal(), request.getConsPoint());
                    }
                }
                //查询个人所在的组织
                entityXfRecordProcess.setDeptId(entityPersonnelInfo.getDeptId());
                if (entityPersonnelInfo.getDeptId() == null) {
                    entityPersonnelInfo.setDeptId(0l);
                }
                EntityDeptInfo entityDeptInfo = deptInfoService.selectDeptName(request.getCompanyId(), entityPersonnelInfo.getDeptId());
                if (entityDeptInfo != null) {
                    entityXfRecordProcess.setDeptName(entityDeptInfo.getDeptName());
                }

                entityXfRecordProcess.setMealDate(DateUtils.getDate(DateUtils.getStrDate(date, DateUtils.YYYY_MM_DD), DateUtils.YYYY_MM_DD));
                //消费状态，1：消费；2：退款；3：点餐；4.取餐；5.订餐；6：退订；10：充值；11：提现；12：补贴；13：清卡；21：销户
                entityXfRecordProcess.setConsState(request.getConsState());
                entityXfRecordProcess.setOrderNo(request.getTradeNo());
                Integer intMealNo = 0;
                if (entityXfRecordProcess.getMealNo() != null) {
                    intMealNo = Integer.valueOf(entityXfRecordProcess.getMealNo());
                }

                //693单位特殊处理  分院仅消费一次  2025年8月1日14:59:09
                try {
                    if (request.getCompanyId() == 693l && entityXfRecordProcess.getPointId() == 1044l) {
                        int sun = xfRecordProcessExxxService.getUserPartCityCount(693l, entityXfRecordProcess.getMealDate(), entityXfRecordProcess.getPId());
//                        log.error("厦门第一医院分院仅消费一次-计算  0wnvEd5c 【{}】【{}】【{}】",request,entityXfRecordProcess,sun);
                        if (sun > 0) {
                            return ResultUtil.error(ResultEnum.Err_3119);
                        }
                    }
                } catch (Exception e) {
                    log.error("厦门第一医院分院仅消费一次-异常  sjObbk6X 【{}】", e);
                }

                //判断扣款
                int consMoney = -request.getAmount();
                //计算金额
                entityXfRecordProcess.setConsMoney(Long.valueOf(consMoney));
                //计算优惠
                entityXfRecordProcess.setConsType(request.getConsType());
                int discount = xfDiscountConfigServcie.selectSiscount(request.getCompanyId(), entityXfAccountInfo.getPId(),
                        entityXfAccountInfo.getAccTypeId(), request.getConsType(), date, intMealNo, request.getAmount(), entityXfRecordProcess);
                consMoney = entityXfRecordProcess.getConsMoney().intValue();
                int realMoney = consMoney + discount;
                //查询支付限制

                //有优惠情况
                int refPayTimes = 0 ;
                String canKey = "onlinePayCard:discountPaying#comId:"+request.getCompanyId()+"#pId:"+entityXfRecordProcess.getPId();
                if (discount != 0) {
                    //1.判断是否有第二笔配置
                    refPayTimes = payHandleFunService.getRefPayTimes(request.getCompanyId());
                    if (refPayTimes > 0) {
                        //2.判断是否有支付中
                        long currentTimeMillis = System.currentTimeMillis();
                        Set<Object> orders = redisUtil.rangeByScore(canKey,currentTimeMillis-refPayTimes*1000l,currentTimeMillis+1000l);
                        if(orders.size()>0){
                            return ResultUtil.info(4024,"存在优惠订单支付中，请等待"); //避免被插入补缴 用旧的code
                        }
                        //3.插入支付中
                        redisUtil.zAdd(canKey,request.getTradeNo(),currentTimeMillis);
                        redisUtil.expire(canKey,3600l*2l);
                    }
                }

                boolean bj = payHandleFunService.checkIfWaitPay(request.getCompanyId(),intMealNo.toString(),entityXfRecordProcess.getMerchId());

                int limit = xfUserparamConfigService.selectLimit(request.getCompanyId(), entityXfAccountInfo.getPId(),
                        entityXfAccountInfo.getAccTypeId(), request.getConsType(), date, intMealNo, (Math.abs(realMoney)),bj);
                if (limit != 0) {
                    if (refPayTimes > 0) {
                        redisUtil.zRemove(canKey,request.getTradeNo());
                    }
                    if (limit == 1) {
                        return ResultUtil.error(ResultEnum.Err_4031);
                    } else if (limit == 2) {
                        return ResultUtil.error(ResultEnum.Err_4032);
                    }
                    if (limit == 3) {
                        return ResultUtil.error(ResultEnum.Err_4033);
                    }
                    if (limit == 4) {
                        return ResultUtil.error(ResultEnum.Err_4034);
                    }
                    if (limit == 5) {
                        return ResultUtil.error(ResultEnum.Err_4035);
                    }
                    if (limit == 6) {
                        return ResultUtil.error(ResultEnum.Err_4036);
                    }
                }
                //支付状态；0支付中、1支付成功，2支付失败，3撤单中，4已撤单，5退款中，6已退款，7部分退款成功
                entityXfRecordProcess.setPayState(0);
                entityXfRecordProcess.setDiscount(Long.valueOf(discount));
                entityXfRecordProcess.setRealMoney(Long.valueOf(realMoney));
                entityXfRecordProcess.setPayAmount(Long.valueOf(Math.abs(realMoney)));

                EntityXfDeviceMealConfig mealConfig = null;
                String mealNoEntiy = "0";
                if (entityXfRecordProcess.getMealNo() != null && request.getConsType()==1) {
                    mealNoEntiy = entityXfRecordProcess.getMealNo();
                }

                //5A、如果设备餐段参数中消费类型、餐段匹配的消费券方式为1-记次模式：
                if (entityXfRecordProcess.getXfDeviceMealConfigList() != null && entityXfRecordProcess.getMealNo() != null) {
                    for (EntityXfDeviceMealConfig entityXfDeviceMealConfig : entityXfRecordProcess.getXfDeviceMealConfigList()) {
                        String mealNo = String.valueOf(entityXfDeviceMealConfig.getMealNo());
                        if (entityXfRecordProcess.getConsType().compareTo(entityXfDeviceMealConfig.getConsType()) == 0 && mealNoEntiy.equals(mealNo)) {
                            if (entityXfDeviceMealConfig.getVoucherMode() == 1) {
                                entityXfRecordProcess.setOpTypeDiff(0L);
                            }
                            mealConfig = entityXfDeviceMealConfig;
                            break;
                        }
                    }
                }

                //查询扣款顺序
                EntityConsTypeConfig entityConsTypeConfig = consTypeConfigService.selectEntityConsTypeConfig(request.getCompanyId(), request.getConsType());

                if(entityConsTypeConfig==null){
                    entityConsTypeConfig=new EntityConsTypeConfig();
                    entityConsTypeConfig.setConsType(request.getConsType());
                    entityConsTypeConfig.setSubAccount(0);
                    entityConsTypeConfig.setWalletSerial("21");
                    entityConsTypeConfig.setWithholdSerial(0);
                }

                int payState = 0;
                String walletSerial = "21";

                //非计次消费
                if (mealConfig == null || mealConfig.getVoucherMode() != 1 && realMoney != 0) {

                    //0-不允许代扣，1代扣优先，2最后代扣
                    int withholdSerial = 0;
                    //字符串：空表示不允许使用虚拟账户扣款,扣款顺序（1现金钱包、2补贴钱包）
                    if (entityConsTypeConfig != null) {
                        if (entityConsTypeConfig.getWithholdSerial() != null) {
                            withholdSerial = entityConsTypeConfig.getWithholdSerial();
                        }
                        if (entityConsTypeConfig.getWalletSerial() != null) {
                            walletSerial = entityConsTypeConfig.getWalletSerial();
                        }
                    }


                    //第三方的支付失败结果
                    JSONObject jsonObject = new JSONObject();
                    //0不允许代扣  1代扣优先  2最后代扣
                    if (entityXfRecordProcess.getRealMoney() == 0) {
                        entityXfRecordProcess.setOpTypeDiff(100l);
                        payState = payFunService.useVoucherAndSubAccount(entityXfRecordProcess,
                                entityConsTypeConfig,
                                mealConfig,
                                request.getMealData(), null, entityXfAccountInfo, null);
                    } else {
                        FeePlanVo feePlanVo = new FeePlanVo();
                        feePlanVo.setWalletFee(Math.abs(entityXfRecordProcess.getRealMoney().intValue()));
                        EntityXfSubAccountList entityXfSubAccountList = null;

                        //工本费
                        Long gbf = 0l;
                        try {
                            gbf = xfAccountTypeService.selectGBF(entityXfRecordProcess.getComId(), entityXfAccountInfo.getAccTypeId());
                        } catch (Exception exception) {
                            StackTraceElement ste = exception.getStackTrace()[0];
                            log.info("查找工本费异常 【{}】【{}】", ste.getLineNumber(), exception);
                        }
                        //当前可支付余额
                        Integer walletBalance = entityXfAccountInfo.getAccountRechargeMoney().intValue() + entityXfAccountInfo.getAccountSubsidyMoney().intValue() - gbf.intValue();

                        if (!walletSerial.equals("") ) {
                            if (entityConsTypeConfig.getSubAccount() == 0) {
                                entityXfRecordProcess.setOpTypeDiff(100l);
                                if (CommonUtil.isNull(entityConsTypeConfig.getWalletSerial())) walletBalance = 0;
                                if (entityConsTypeConfig.getWalletSerial().equals("1"))
                                    walletBalance = entityXfAccountInfo.getAccountRechargeMoney().intValue() - gbf.intValue();
                                if (entityConsTypeConfig.getWalletSerial().equals("2"))
                                    walletBalance = entityXfAccountInfo.getAccountSubsidyMoney().intValue();
                            } else {
                                entityXfRecordProcess.setOpTypeDiff(100l + entityConsTypeConfig.getSubAccount());
                                entityXfSubAccountList = xfSubAccountListService.getUserSubAccount(
                                        entityXfRecordProcess.getComId(),
                                        entityXfRecordProcess.getPId(),
                                        entityConsTypeConfig.getSubAccount()
                                );
                                walletBalance = entityXfSubAccountList.getAccountSubsidyMoney() + entityXfSubAccountList.getAccountRechargeMoney();
                                if (CommonUtil.isNull(entityConsTypeConfig.getWalletSerial())) walletBalance = 0;
                                if (entityConsTypeConfig.getWalletSerial().equals("1"))
                                    walletBalance = entityXfSubAccountList.getAccountRechargeMoney();
                                if (entityConsTypeConfig.getWalletSerial().equals("2"))
                                    walletBalance = entityXfSubAccountList.getAccountSubsidyMoney();
                            }

                            feePlanVo = payFunService.getVoucherUse(entityXfRecordProcess,
                                    entityConsTypeConfig,
                                    mealConfig,
                                    request.getMealData(), walletBalance);

                        }

                        if (withholdSerial == 0) {
                            if (walletSerial.equals("")) {
                                return ResultUtil.error(ResultEnum.Err_4038);
                            }
                            if (walletBalance.compareTo(feePlanVo.getWalletFee())<0) {
                                //余额不足
                                return ResultUtil.error(ResultEnum.Err_4024);
                            }
                            //不允许代扣
                            payState =  payFunService.useVoucherAndSubAccount(entityXfRecordProcess,
                                    entityConsTypeConfig,
                                    mealConfig,
                                    request.getMealData(), feePlanVo, entityXfAccountInfo, entityXfSubAccountList);
                        } else {
                            //字符串：空表示不允许使用虚拟账户扣款
                            if (walletSerial.equals("")) {
                                payState = payFunService.withholding(entityXfRecordProcess, request, entityCardInfo, jsonObject);
                                if (payState == 2) {
                                    //支付失败
                                    if (refPayTimes > 0) {
                                        redisUtil.zRemove(canKey,request.getTradeNo());
                                    }
                                    return ResultUtil.info(4024, jsonObject.getString("msg"));
                                }
                            } else {
                                if (withholdSerial == 1) {
                                    //1代扣优先
                                    payState = payFunService.withholding(entityXfRecordProcess, request, entityCardInfo, jsonObject);
                                    if (payState != 0 && payState != 1 && payState != -11) {
                                        entityXfRecordProcess.setOpTypeDiff(null);
                                        if (walletBalance.compareTo(feePlanVo.getWalletFee())<0) {
                                            //余额不足
                                            if (refPayTimes > 0) {
                                                redisUtil.zRemove(canKey,request.getTradeNo());
                                            }
                                            return ResultUtil.error(ResultEnum.Err_4024);
                                        }
                                        entityXfRecordProcess.setOpTypeDiff(100l);
                                        payState =  payFunService.useVoucherAndSubAccount(entityXfRecordProcess,
                                                entityConsTypeConfig,
                                                mealConfig,
                                                request.getMealData(), feePlanVo, entityXfAccountInfo, entityXfSubAccountList);
                                    }
                                } else {
                                    // 2最后代扣
                                    if (walletBalance.compareTo(feePlanVo.getWalletFee())>=0) {
                                        entityXfRecordProcess.setOpTypeDiff(100l);
                                        payState =  payFunService.useVoucherAndSubAccount(entityXfRecordProcess,
                                                entityConsTypeConfig,
                                                mealConfig,
                                                request.getMealData(), feePlanVo, entityXfAccountInfo, entityXfSubAccountList);
                                    }
                                    if (payState != 1) {
                                        payState = payFunService.withholding(entityXfRecordProcess, request, entityCardInfo, jsonObject);
                                    }
                                    if (payState == 2) {
                                        //支付失败
                                        if (refPayTimes > 0) {
                                            redisUtil.zRemove(canKey,request.getTradeNo());
                                        }
                                        return ResultUtil.info(3030, jsonObject.getString("msg"));
                                    }
                                }
                            }
                        }
                    }
                } else {
                    entityXfRecordProcess.setOpTypeDiff(100l);
                    //计次消费
                    if(mealConfig != null && mealConfig.getVoucherMode() == 1 ){
                        entityXfRecordProcess.setOpTypeDiff(0l);
                    }
                    entityXfRecordProcess.setChargeMoney(0L);
                    entityXfRecordProcess.setSubMoney(0L);
                    entityXfRecordProcess.setSubBalance(entityXfAccountInfo.getAccountSubsidyMoney());
                    entityXfRecordProcess.setChargeBalance(entityXfAccountInfo.getAccountRechargeMoney());

                    payState = payFunService.useVoucherAndSubAccount(entityXfRecordProcess,
                            entityConsTypeConfig,
                            mealConfig,
                            request.getMealData(), null, entityXfAccountInfo, null);
                }

                if (refPayTimes > 0) {
                    redisUtil.zRemove(canKey,request.getTradeNo());
                }

                if (payState == 1) {
                    //支付成功
                    Map<String, Object> map = new HashMap<>();
                    map.put("discount", entityXfRecordProcess.getDiscount());
                    map.put("payAmount", Math.abs(entityXfRecordProcess.getRealMoney()));
                    Map<String, Object> mapAccountInf = new HashMap<>();
                    mapAccountInf.put("name", entityXfRecordProcess.getPName());
					/*if (entiyEntiyXfRecordProcess.getChargeBalance()!=null && entiyEntiyXfRecordProcess.getSubBalance()!=null){
						mapAccountInf.put("money", entiyEntiyXfRecordProcess.getChargeBalance()+entiyEntiyXfRecordProcess.getSubBalance());
					}else {
						if (entiyEntiyXfRecordProcess.getChargeBalance()!=null){
							mapAccountInf.put("money", entiyEntiyXfRecordProcess.getChargeBalance());
						}
						if (entiyEntiyXfRecordProcess.getSubBalance()!=null){
							mapAccountInf.put("money", entiyEntiyXfRecordProcess.getSubBalance());
						}
					}
					mapAccountInf.put("allowance",entityXfAccountInfo.getAccountSubsidyMoney());*/
                    mapAccountInf.put("money", entityXfRecordProcess.getChargeBalance());
                    mapAccountInf.put("allowance", entityXfRecordProcess.getSubBalance());
                    if (entityCardInfo != null) {
                        mapAccountInf.put("expireDate", DateUtils.getStrDate(entityCardInfo.getEndDate(), DateUtils.YYYY_MM_DD_HH_MM_SS));
                    }
                    mapAccountInf.put("cardtype", entityXfAccountInfo.getAccTypeId());
                    map.put("accountInf", mapAccountInf);
                    map.put("pickupNum", entityXfRecordProcess.getReceiptNo());
                    map.put("orderTime", DateUtil.getDateTime(entityXfRecordProcess.getPayTime()));
                    return ResultUtil.success(map);
                } else if (payState == -1) {
                    //商户支付渠道未配置"
                    return ResultUtil.error(ResultEnum.Err_3048);
                } else if (payState == -2) {
                    //代扣未签约"
                    return ResultUtil.error(ResultEnum.Err_3047);
                } else if (payState == -11) {
                    //订单号重复
                    return ResultUtil.error(ResultEnum.Err_802);
                } else if (payState == -12) {
                    //余额不足
                    return ResultUtil.error(ResultEnum.Err_4024);
                } else if (payState == -22) {
                    //转退款
                    return ResultUtil.info(4024,"优惠订单转入退款");
                } else {
                    //支付结果未知
                    return ResultUtil.error(ResultEnum.Err_3029);
                }
            } else {
                log.info("请求方式66:【{}】,机器号:【{}】,请求参数:【{}】,返回:【{}】", "支付请求（强在线）", request.getMachNo(), request, ResultUtil.error(ResultEnum.Err_802));
                //订单号重复
                return ResultUtil.error(ResultEnum.Err_802);
            }
        } catch (Exception e) {
            StackTraceElement ste = e.getStackTrace()[0];
            log.info("请求方式67:【{}】,机器号:【{}】,请求参数:【{}】,异常2:【{}】,行号:【{}】", "支付请求（强在线）", request.getMachNo(), request, e, ste.getLineNumber());
            return ResultUtil.error(ResultEnum.Err_3031);
        }
    }


    //随机选择灰度or正式
    @PostMapping(PayHandleUrl.URL_chooseClient)
    public  Integer chooseClient(){
        return new Random().nextInt(2);
    }

}

